﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cricket.LinqData;
using Cricket.LinqModel;

public partial class AddSeries : System.Web.UI.Page
{
    #region Variable Declaration
    IDataRepository _dataObject = new DataRepository();
    Series series;
    List<Series> lstSeries;
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

            HttpCookie cookie = Request.Cookies["Admin"];
            if (cookie == null)
            {
                Response.Redirect("Login.aspx");
            }
            else
            {

                if (Request.QueryString["Id"] != null)
                {
                    fillSeries(Request.QueryString["Id"].ToString());
                }
            }
        }
    }

    private void fillSeries(string SeriesId)
    {
        series = new Series(_dataObject);
        series.Id = Convert.ToInt32(SeriesId);
        series.GetSeriesById();
        if (series.Id != 0)
        {
            txtTeamName.Text = series.Name;
            txtAbbrivation.Text = series.Abbrivation;
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        series = new Series(_dataObject);
        series.Name = txtTeamName.Text;
        series.Abbrivation = txtAbbrivation.Text;        
        bool teamNameExist = CheckTeamNameExist(txtTeamName.Text);
        bool abbrevationNameExist = CheckAbbrevationExist(txtAbbrivation.Text);
        series.IsActive = true;
        series.IsDeleted = false;
        if (Request.QueryString["Id"] != null)
        {
            if (teamNameExist)
            {
                layerError.InnerHtml = "<strong>Error!</strong> Series already exist."; ;
                layerError.Attributes.Add("style", "display:block;");
                layerSuccess.Attributes.Add("style", "display:none;");
            }
            else if (abbrevationNameExist)
            {
                layerError.InnerHtml = "<strong>Error!</strong> Abbrevation already exist."; ;
                layerError.Attributes.Add("style", "display:block;");
                layerSuccess.Attributes.Add("style", "display:none;");
            }
            else
            {
                series.UpdatedDate = System.DateTime.UtcNow;
                series.Id = Convert.ToInt32(Request.QueryString["Id"].ToString());
                series.UpdateSeries();
                Response.Redirect("ViewSeries.aspx");
            }

        }
        else
        {
            if (teamNameExist)
            {
                layerError.InnerHtml = "<strong>Error!</strong> Series already exist."; ;
                layerError.Attributes.Add("style", "display:block;");
                layerSuccess.Attributes.Add("style", "display:none;");
            }
            else if (abbrevationNameExist)
            {
                layerError.InnerHtml = "<strong>Error!</strong> Abbrevation already exist."; ;
                layerError.Attributes.Add("style", "display:block;");
                layerSuccess.Attributes.Add("style", "display:none;");
            }
            else
            {
                series.CreatedDate= System.DateTime.UtcNow;
                series.Create();
                clearControl();
                layerError.Attributes.Add("style", "display:none;");

                layerSuccess.InnerHtml = "<strong>Success!</strong> Series added successfully.";
                layerSuccess.Attributes.Add("style", "display:block;");
            }
        }
    }

    private void clearControl()
    {
        txtTeamName.Text = string.Empty;
        txtAbbrivation.Text = string.Empty;
    }

    private bool CheckAbbrevationExist(string abbrevation)
    {
        bool isExist = false;
        int SeriesId = 0;
        if (Convert.ToString(Request.QueryString["Id"]) != "" && Request.QueryString["Id"] != null)
        {
            SeriesId = Convert.ToInt32(Request.QueryString["Id"]);
        }
        lstSeries = new List<Series>();
        lstSeries = series.CheckSeriesAbbrivationExist(SeriesId, abbrevation);
        if (lstSeries.Count > 0)
        {
            isExist = true;
        }
        else
        {
            isExist = false;
        }
        return isExist;
    }

    private bool CheckTeamNameExist(string teamName)
    {
        bool isExist = false;
        int SeriesId = 0;
        if (Convert.ToString(Request.QueryString["Id"]) != "" && Request.QueryString["Id"] != null)
        {
            SeriesId = Convert.ToInt32(Request.QueryString["Id"]);
        }
        lstSeries = new List<Series>();
        lstSeries = series.CheckSeriesNameExist(SeriesId, teamName);
        if (lstSeries.Count > 0)
        {
            isExist = true;
        }
        else
        {
            isExist = false;
        }
        return isExist;
    }
    protected void btnView_Click(object sender, EventArgs e)
    {
        Response.Redirect("ViewSeries.aspx");
    }
}